﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.DomainModel;
using Enterprise.Framework.DomainModel.Repositories;
using Enterprise.Framework.Core.Exceptions;
using Enterprise.Framework.DomainModel.Specifications;
using Resto.CostControl.Core.Domain.Resource;
using Resto.CostControl.Common.Utility;

namespace Resto.CostControl.Core.Domain.Model
{
    /// <summary>
    /// 供应商合同
    /// </summary>
    public partial class SupplierContract : IAggregationRoot
    {
        #region 构造函数

        internal SupplierContract() {
            this.ID = IDHelper.CreateNewID();
        }

        #endregion

        #region 私有属性

        private IEntityRepository<SupplierContract> _Repository;
        private IEntityRepository<SupplierContract> Repository
        {
            get
            {
                if (_Repository == null)
                    _Repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<SupplierContract>();

                return _Repository;
            }
        }

        #endregion

        #region 业务方法

        internal void Add()
        {
            Validate();

            this.CreateAudit = new Audit();
            this.LastUpdateAudit = new Audit();

            this.Repository.Add(this);
        }

        internal void Update()
        {
            Validate();

            this.LastUpdateAudit = new Audit();

            this.Repository.Update(this);
        }

        internal void Delete()
        {
            this.Repository.Delete(this);
        }

        internal void AddItem(SupplierContractItem item)
        {
            bool result = this.Items.Where(p => p.MaterialID == item.MaterialID).Count() > 0;
            if (result)
                throw new BusinessValidationException(SupplierResource.ValidateException_ContractItem_Repeat);

            this.Items.Add(item);
        }

        internal void ClearItems()
        {
            SupplierContractItem[] array = this.Items.ToArray();
            foreach (SupplierContractItem item in array)
                item.Delete();

            this.Items.Clear();
        }

        #endregion

        #region 验证方法

        protected void Validate()
        {
            List<string> messageList = new List<string>();

            ValidateTimeRepeat(messageList);
            ValidateCodeRepeat(messageList);

            if (messageList.Count > 0)
                throw new BusinessValidationException(messageList.ToArray());
        }

        /// <summary>
        /// 验证指定时间段内是否有重复的合同
        /// </summary>
        /// <param name="messageList"></param>
        private void ValidateTimeRepeat(IList<string> messageList)
        {
            ISpecification<SupplierContract> spec = Specification<SupplierContract>.Create(
                p =>  (p.BeginDate >= this.BeginDate && p.BeginDate <= this.EndDate)
                       ||
                       (p.EndDate >= this.BeginDate && p.EndDate <= this.EndDate)                    
                    );
            spec = spec.And(Specification<SupplierContract>.Create(p => p.ID != ID));

            bool result = Repository.Count(spec) > 0;
            if (result)
                messageList.Add(string.Format(SupplierResource.ValidateException_ContractTime_Repeat));
        }

        private void ValidateCodeRepeat(IList<string> messageList)
        {
            ISpecification<SupplierContract> spec = Specification<SupplierContract>.Create(p => p.Code == this.Code);
            spec = spec.And(Specification<SupplierContract>.Create(p => p.ID != ID));

            bool result = Repository.Count(spec) > 0;
            if (result)
                messageList.Add(string.Format(SupplierResource.ValidateException_ContractCode_Repeat, this.Code));
        }

        #endregion
    }
}
